﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSTSCLib;

namespace DMSMT.Tool
{
    public partial class RDPSubFrm : Form
    {
        public string Server{get;set;}
        public string UserName { get; set; }
        public string Pwd { get; set; }
        public int Port { get; set; }
        public bool EnableCredSspSupport { get; set; }

        //private AxMSTSCLib.AxMsRdpClient7 rdpClient = new AxMSTSCLib.AxMsRdpClient7();
        //private AxMSTSCLib.AxMsRdpClient7 rdpClient = new AxMSTSCLib.AxMsRdpClient7();
        public RDPSubFrm()
        {
            InitializeComponent();
            Port = 3389;
            EnableCredSspSupport = false;
            //rdpClient.OnConnected+=new EventHandler(RDPClient_OnConnected);
        }

        private void RDPClient_OnConnected(object sender, EventArgs e)
        {
            Log.LogText(Server+"\t"+"连接成功");
        }

         //rdp.Server = ip;
         //       rdp.AdvancedSettings2.RDPPort = 3389;
         //       rdp.Height = rect.Height;
         //       rdp.Width = rect.Width;
         //       rdp.UserName = "administrator";
         //       rdp.AdvancedSettings2.ClearTextPassword = "123";
         //       rdp.Connect();
         //       rdp.FullScreen = true;
         //       //this.FormBorderStyle = FormBorderStyle.None;
         //       rdp.FullScreenTitle = this.Text;

        public void Connect()
        {
            try
            {
                // 获取主机显示器屏幕分辨率
                Rectangle rect = Screen.PrimaryScreen.Bounds;
                Log.LogText("开始连接远程桌面" + Server);
                /*  rdp.Server = Server;  //乐博网提示  这里是输入服务器的ip地址或者域名
                  rdp.UserName = UserName;   // 这里是输入服务器的登录用户 比如administrator
                  //Log.LogText(this.Height + "\t" + this.Width);
                  rdp.Size = new Size(rect.Width-17, rect.Height-85);
             
                  IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
                  secured.ClearTextPassword = Pwd;  //这里输入服务器密码
                  rdp.Connect();*/

                
                //rdpClient.Dock = DockStyle.Fill;

                //((System.ComponentModel.ISupportInitialize)(this.rdpClient)).BeginInit();
                //this.Controls.Add(rdpClient);
                //((System.ComponentModel.ISupportInitialize)(this.rdpClient)).EndInit();
                // this.Controls.Add(rdpClient);
                rdpClient.Size = new Size(rect.Width - 17, rect.Height - 85);
                rdpClient.Server = Server;//服务器地址   
                rdpClient.UserName = UserName;
                rdpClient.AdvancedSettings2.RDPPort = Port;
                rdpClient.AdvancedSettings2.ClearTextPassword = Pwd; //密码   
                //rdpClient.AdvancedSettings6
                //rdpClient.ColorDepth = 16;//颜色位数   
                //rdpClient.FullScreen = true;//是否全屏   
                //rdpClient.AdvancedSettings8.PublicMode = true;
                if (EnableCredSspSupport)
                    rdpClient.AdvancedSettings7.EnableCredSspSupport = true;

                rdpClient.Connect();
            }
            catch (Exception Ex)
            {
                Log.LogError("不能连接到远程桌面 " + Server + " 错误信息:  " + Ex.Message);
            }
        }

        public void RConnect()
        {
            RDPAttrsFrm frm = new RDPAttrsFrm(this);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Connect();
            }
        }        

        private void RDPSubFrm_Load(object sender, EventArgs e)
        {
            //Connect();
        }

        private void contextMenu_Click(object sender, EventArgs e)
        {
        }
    }
}
